package com.wonderli.wondertweet;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

import com.wonderli.wondertweet.Prefs;
import com.wonderli.wondertweet.R;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateStatus extends Activity {    
    public final String TAG = getClass().getName();
    private EditText updateText;
    private Button updateBtn;    
    private Twitter twitter;
    private ConfigurationBuilder cb;
    private SharedPreferences settings;
    private String accesstoken;
    private String secrettoken;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updatestatus);
        updateText = (EditText) findViewById(R.id.update_status_text);        
        updateBtn = (Button) findViewById(R.id.update_btn);        
        updateBtn.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				settings = getSharedPreferences(Constants.SETTINGS,0);				
				accesstoken = settings.getString("accesstoken", "default");
				secrettoken = settings.getString("secrettoken", "default");
				System.out.println(accesstoken);
				System.out.println(secrettoken);
				if(accesstoken.equalsIgnoreCase("default")) {
					Intent intent = new Intent(UpdateStatus.this, Prefs.class);
					Toast.makeText(UpdateStatus.this, "You need to Oauth this application", 10000).show();
					startActivity(intent);					
				} else {
/*					Intent intent = getIntent();
					Bundle bundle = intent.getExtras();
					accesstoken = bundle.getString("accesstoken");				
					secrettoken = bundle.getString("secrettoken");*/
					cb = new ConfigurationBuilder();					
					cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY).setOAuthConsumerSecret(Constants.CONSUMER_SECRET).setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(secrettoken);					
					twitter = new TwitterFactory(cb.build()).getInstance();
					try {
						twitter.updateStatus(updateText.getText().toString());
					} catch (TwitterException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}									
				}
								
			}			
        });
		
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if(requestCode == Constants.REQUEST_CODE) {
    		if(resultCode == Constants.RESULT_CODE) {
    			Intent intent = getIntent();
				Bundle bundle = intent.getExtras();
				accesstoken = bundle.getString("accesstoken");				
				secrettoken = bundle.getString("secrettoken");
				cb = new ConfigurationBuilder();					
				cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.CONSUMER_KEY).setOAuthConsumerSecret(Constants.CONSUMER_SECRET).setOAuthAccessToken(accesstoken).setOAuthAccessTokenSecret(secrettoken);					
				twitter = new TwitterFactory(cb.build()).getInstance();
				try {
					twitter.updateStatus(updateText.getText().toString());
				} catch (TwitterException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}    			
    		}
    	}
    }
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater=getMenuInflater();
    	inflater.inflate(R.layout.menu, menu);
    	return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()){
    	case R.id.account_settings:
    		startActivity(new Intent(this, Prefs.class));
    		return true;
    	}
    	return false;
    }
}